#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QMainWindow>
#include <QTableView>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QPersistentModelIndex>
#include <QModelIndex>
#include <QIODevice>
#include <QSslError>
#include <QAction>
#include <QSortFilterProxyModel>

#include "model.h"

class Controller : public QMainWindow
{
    Q_OBJECT
public:
    explicit Controller(Model* model, QWidget* parent = 0);

signals:
    
public slots:
    void replyFinished(QNetworkReply* reply);
    void downloadProgress(qint64 bytes_received, qint64 bytes_total);
    void	 error(QNetworkReply::NetworkError code);
    void sslErrors(const QList<QSslError> &errors);
    void modelChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
    void addTask();
    void exec() {
        startNext();
    }

private:
    void startNext();
    void abortCurrent();
    void wipeCurrent();
    void stopCurrent();
    bool isDownloading();
    bool saveFile(QString path, QIODevice* device);
    QModelIndex indexToCurrent(TaskFields field);
    int findNext();

private:
    QTableView* view_;
    QSortFilterProxyModel* sorting_model_;
    Model* model_;
    QNetworkAccessManager* manager_;
    QNetworkReply* cur_reply_;
    int cur_progress_;
    int cur_row_;
    QAction* add_task_action_;


    QMenu *file_menu_;
};

#endif // CONTROLLER_H
